JavaScript Operators

Types of JavaScript Operators

1. Arithmetic Operators


<!DOCTYPE html>
<html>
<body>
    <h2>Arithmetic Operators</h2>
    <script>                        
        let a = 10, b = 5;
        console.log(a + b); // Output: 15
        console.log(a - b); // Output: 5
        console.log(a * b); // Output: 50
        console.log(a / b); // Output: 2
        console.log(a % b); // Output: 0
</script>
</body>
</html>                            
                        

2. Assignment Operators


<!DOCTYPE html>
<html>
<body>
    <h2>Assignment Operators</h2>
    <script>                            
      let x = 5;
      x += 2; // x = x + 2
      console.log(x); // Output: 7
      x *= 3; // x = x * 3
      console.log(x); // Output: 21
   </script>
</body>
</html>                          
                        

3. Comparison Operators


<!DOCTYPE html>
<html>
<body>
    <h2>Comparison Operators</h2>
    <script>                          
        let a = 10, b = '10';
        console.log(a == b); // Output: true (loose equality)
        console.log(a === b); // Output: false (strict equality)
        console.log(a != b); // Output: false
        console.log(a !== b); // Output: true
        console.log(a > b); // Output: false
     </script>
</body>
</html>                         
                        

4. Logical Operators


<!DOCTYPE html>
<html>
<body>
    <h2>Logical Operators</h2>
    <script>                         
       let a = true, b = false;
       console.log(a && b); // Output: false
       console.log(a || b); // Output: true
       console.log(!a); // Output: false
    </script>
</body>
</html>                         
                        

5. Bitwise Operators


<!DOCTYPE html>
<html>
<body>
    <h2>Bitwise Operators</h2>
    <script>                        
        let a = 5, b = 3; // 5 = 0101, 3 = 0011
        console.log(a & b); // Output: 1 (0001)
        console.log(a | b); // Output: 7 (0111)
        console.log(a ^ b); // Output: 6 (0110)
        console.log(~a); // Output: -6 (inverts bits)
        console.log(a << 1); // Output: 10 (1010)
        console.log(a >> 1); // Output: 2 (0010)
     </script>
</body>
</html>    
                    

6. Ternary Operator


<!DOCTYPE html>
<html>
<body>
    <h2>Ternary Operators</h2>
    <script>                           
        let age = 18;
        let result = (age >= 18) ? "Adult" : "Minor";
        console.log(result); // Output: "Adult"
    </script>
</body>
</html>                            
                        

7. typeof Operator


<!DOCTYPE html>
<html>
<body>
    <h2>typeof Operators</h2>
    <script>                          
        let num = 5;
        console.log(typeof num); // Output: "number"
    </script>
</body>
</html>